/* Copyright  2009
   @Author
   Richard Changde Yin            e-mail yinchangde@hotmail.com

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/**
 * filename: single_thread.cc
 */

#include <include/server_includes.h>
#include <include/gettext.h>
#include <include/error.h>
#include <include/plugin/scheduler.h>

#include <include/connect.h>
#include <include/sql_parse.h>
#include <include/session.h>


#include <string>
using namespace std;

class Single_thread_scheduler : public Scheduler
{
public:
  Single_thread_scheduler(uint32_t threads): Scheduler(threads)
  {}

  virtual bool init_new_connection_thread(void) {return 0;}

  /*
    Simple scheduler that use the main thread to handle the request

    NOTES
    This is only used for debugging...

    When we enter this function, LOCK_thread_count is held!
  */

  virtual bool add_connection(Session* session)
  {
    handle_one_connection((void*) session);

    return false;
  }


  /*
    End connection, in case when we are using 'no-threads'
  */

  virtual bool end_thread(Session *session, bool)
  {
  /*  unlimk_session : declared in the main.cc*/
    unlink_session(session);   /* locks LOCK_thread_count and deletes session */

    return true;               // Abort handle_one_connection
  }


  virtual uint32_t count(void)
  {
    return 0;
  }

};

static int init(void *p)
{
  Scheduler **sched= static_cast<Scheduler **>(p);
  *sched= new Single_thread_scheduler(1);

  return 0;
}

static int deinit(void *p)
{
  Scheduler *sched= static_cast<Scheduler *>(p);
  delete sched;
  return 0;
}

static struct st_sql_sys_var* system_variables[]= {
  NULL
};

collapsar_declare_plugin(single_thread)
{
  COLLAPSAR_SCHEDULING_PLUGIN,
  "single_thread",
  "0.1",
  "N/A",
  "Single Thread Scheduler",
  PLUGIN_LICENSE_GPL,
  init,   /* Plugin Init */
  deinit, /* Plugin Deinit */
  NULL,   /* status variables */
  system_variables,   /* system variables */
  NULL    /* config options */
}
collapsar_declare_plugin_end;
